package com.ly.mpayment.rest.init;

import java.util.List;

import com.ly.mpayment.core.util.CookieRedis;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.ly.mpayment.core.util.RedisUtil;
import com.ly.mpayment.persist.entity.SysConfig;
import com.ly.mpayment.persist.entity.yidong.Payment;
import com.ly.mpayment.persist.service.sys.ISysConfigService;
import com.ly.mpayment.persist.service.yidong.IPaymentService;

import redis.clients.jedis.Jedis;

@Component
public class SystemInit implements InitializingBean{
	static Logger logger = Logger.getLogger(SystemInit.class);
	@Autowired
	ISysConfigService configService;
	@Autowired
	IPaymentService payMentService;
	@Override
	public void afterPropertiesSet() throws Exception {
		initRedis();
		//initPayMent();
		logger.info("初始化配置完成....");
	}

	/**
	 * 初始化redis
	 */
	private void initRedis(){
		List<SysConfig> configs = configService.findAll();
		Jedis jedis = RedisUtil.getInstance().getJedis();
		if(jedis != null){
			for(SysConfig config : configs){
				jedis.set(CookieRedis.key+config.getAttr(), config.getAttrValue());
				logger.info(config.getAttr()+"="+config.getAttrValue());
			}
			RedisUtil.returnBrokenResource(jedis);
		}else{
			logger.info("redis配置错误");
		}
	}

	private void initPayMent(){
		List<Payment> list = payMentService.findAll();
		Jedis jedis = RedisUtil.getInstance().getJedis();
		if(jedis != null){
			for(Payment pay : list){
				jedis.set(pay.getShopNum(), JSONObject.toJSONString(pay));
				logger.info(pay.getShopNum()+"="+JSONObject.toJSONString(pay));
			}
			RedisUtil.returnBrokenResource(jedis);

			logger.info("initPayMent初始化配置....");
		}else{
			logger.info("redis配置错误");
		}
	}

}
